package com.shenmazong.scheduling;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.shenmazong.config.DayNumConfig;
import com.shenmazong.dto.TbCompany;
import com.shenmazong.mapper.TbCompanyMapper;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author 阿瑞
 * @version 1.0
 * @description TODO
 * @class DayNumScheduling ???
 * @date 2021/9/27 8:14
 */
@Component
public class DayNumScheduling {
    @Autowired
    private TbCompanyMapper tbCompanyMapper;
    @Autowired
    private AmqpTemplate amqpTemplate;
    private ObjectMapper objectMapper=new ObjectMapper();
    public void dayNumCheck(){
        List<TbCompany> list = tbCompanyMapper.selectList(new QueryWrapper<TbCompany>()
                .lambda().eq(TbCompany::getDeleteFlag, 0).le(TbCompany::getDayNum, 0));
        for (TbCompany company : list) {
            company.setDayNum(company.getDayNum()-1);
            tbCompanyMapper.updateById(company);
            if(company.getDayNum()<=3){
                try {
                    amqpTemplate.convertAndSend(DayNumConfig.NUM_EXCHANGE,DayNumConfig.NUM_DEAD_KEY,objectMapper.writeValueAsString(company));
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
